<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_closestFacilitiesService"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
            border: 1px solid #3473b7;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            width: 320px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_closestFacilitiesService"></h5></div>
    <div class='panel-body content'>
        <div class='panel'>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_weightField"></span>
                <select class='form-control' id='edgeWeightNames'>
                    <option>length</option>
                    <option>time</option>
                </select>
            </div>
        </div>
        <div class='panel'>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_turnField"></span>
                <select class='form-control' id='turnNodeWeightNames'>
                    <option>TurnCost</option>
                </select>
            </div>
        </div>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_eventPoint" onclick="selectEventPoint()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_submit" onclick="findClosestFacilities()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear" onclick="clearElements()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
    var  map, layer, vectorLayer, markerLayer1,
        markerLayer2, drawPoint, eventPoint, n = 0,
        facilityPoints = [
            new SuperMap.Geometry.Point(6000, -5500),
            new SuperMap.Geometry.Point(5500, -2500),
            new SuperMap.Geometry.Point(2500, -3500)],
        style = {
            strokeColor: "#304DBE",
            strokeWidth: 3,
            pointerEvents: "visiblePainted",
            fill: false
        },
        url1 = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        url2 = host + "/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";

    init();

    function init() {
        vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
        drawPoint = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Point);
        drawPoint.events.on({"featureadded": drawCompleted});
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                }),
                drawPoint], units: "m"
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("Changchun", url1, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        layer.events.on({"layerInitialized": addLayer});
        markerLayer1 = new SuperMap.Layer.Markers("Markers");
        markerLayer2 = new SuperMap.Layer.Markers("Markers");

        var size = new SuperMap.Size(44, 40),
            offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
            icon1 = new SuperMap.Icon("./images/marker-gold.png", size, offset),
            icon2 = new SuperMap.Icon("./images/marker-gold.png", size, offset),
            icon3 = new SuperMap.Icon("./images/marker-gold.png", size, offset);
        markerLayer1.addMarker(new SuperMap.Marker(new SuperMap.LonLat(6000, -5500), icon1));
        markerLayer1.addMarker(new SuperMap.Marker(new SuperMap.LonLat(5500, -2500), icon2));
        markerLayer1.addMarker(new SuperMap.Marker(new SuperMap.LonLat(2500, -3500), icon3));
    }

    function addLayer() {
        map.addLayers([layer, vectorLayer, markerLayer1, markerLayer2]);
        map.setCenter(new SuperMap.LonLat(4503.6240321526, -3861.911472192499), 1);
    }

    function selectEventPoint() {
        widgets.alert.clearAlert();
        clearElements();
        drawPoint.activate();
    }

    function drawCompleted(drawGeometryArgs) {
        var point = drawGeometryArgs.feature.geometry,
            size = new SuperMap.Size(44, 33),
            offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
            icon = new SuperMap.Icon("./images/marker.png", size, offset);
        markerLayer2.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
        eventPoint = point;
        n++;
        if (n >= 1) {
            drawPoint.deactivate();
        }
    }

    function findClosestFacilities() {
        widgets.alert.clearAlert();
        drawPoint.deactivate();
        if (!eventPoint) {
            widgets.alert.showAlert(resources.msg_eventPoint,true);
            return;
        }
        var findClosestFacilitiesService, parameter, analystParameter, resultSetting;
        resultSetting = new SuperMap.REST.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        var edgeWeightNamesSelect = document.getElementById("edgeWeightNames");
        var weightFieldName = edgeWeightNamesSelect.options[edgeWeightNamesSelect.selectedIndex].text;

        var turnWeightFieldSelect = document.getElementById("turnNodeWeightNames");
        var turnNodeWeightNames = turnWeightFieldSelect.options[turnWeightFieldSelect.selectedIndex].text;
        analystParameter = new SuperMap.REST.TransportationAnalystParameter({
            resultSetting: resultSetting,
            turnWeightField: turnNodeWeightNames,
            weightFieldName: weightFieldName
        });
        parameter = new SuperMap.REST.FindClosestFacilitiesParameters({
            event: eventPoint,
            expectFacilityCount: 1,
            isAnalyzeById: false,
            facilities: facilityPoints,
            parameter: analystParameter
        });
        findClosestFacilitiesService = new SuperMap.REST.FindClosestFacilitiesService(url2, {
            eventListeners: {"processCompleted": processCompleted}
        });
        findClosestFacilitiesService.processAsync(parameter);
    }

    function processCompleted(findClosestFacilitiesEventArgs) {
        var result = findClosestFacilitiesEventArgs.result,
            features = [];
        if (result.facilityPathList) {
            for (var i = 0, facilityPathList = result.facilityPathList, len = facilityPathList.length; i < len; i++) {
                var feature = new SuperMap.Feature.Vector();
                feature.geometry = facilityPathList[i].route;
                feature.style = style;
                features.push(feature);
            }
        }
        vectorLayer.addFeatures(features);
    }

    function clearElements() {
        widgets.alert.clearAlert();
        eventPoint = null;
        n = 0;
        markerLayer2.clearMarkers();
        vectorLayer.removeAllFeatures();
    }
</script>

</body>
</html>